//index.js
//获取应用实例
const app = getApp()
const domain = app.globalData.domain;
let _this;
Page({
  data: {
    schoolYear: wx.getStorageSync('res').schoolYear,
    schoolTerm: wx.getStorageSync('res').schoolTerm,
    // 导航栏和状态栏高度
    navigationBarAndStatusBarHeight:
      wx.getStorageSync('statusBarHeight') +
      wx.getStorageSync('navigationBarHeight') +'px',
      
    imgurls:[
      {cover:'http://tieba.work211.cn/plugins/mok_bgimg/img/3.jpg'},
      {cover:'http://tieba.work211.cn/plugins/mok_bgimg/img/3.jpg'},
      {cover:'http://tieba.work211.cn/plugins/mok_bgimg/img/3.jpg'},
  ],
    isFirst:true,
    list:[
    ],
    server:[]
  },
  navToArea(){
    wx.navigateTo({
      url: '/pages/area/area',
    })
  },
  navTo2(e){
    app.com.navTo(e)
  },
  navTo(e) {
    if (wx.getStorageSync("user").phone == '' || wx.getStorageSync("user").phone == null || wx.getStorageSync("user").phone == undefined){
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }else{
      let name = e.currentTarget.dataset.name
      let index = e.currentTarget.dataset.index
      if(this.data.list[index].is_show == 1){
        if (name == '取快递') {
          wx.navigateTo({
            url: '/pages/daiqu/daiqu?index=' + index,
          })
        }else if (name == '课表') {
          let res = wx.getStorageSync('res')
          if(res!=0){
            wx.navigateTo({
              url: '/pages/educational/course/course?index=' + index,
            })
          }else{
            wx.showToast({
              title: '请绑定学号',
              icon:'error'
            })
            setTimeout(() => {
              wx.navigateTo({
                url: '/pages/educational/edu/edu',
              })
            }, 500);
          }
        } else if (name == '打印服务') {
          wx.navigateTo({
            url: '/pages/dayin/dayin?index=' + index,
          })
        } else {
          wx.navigateTo({
            url: '/pages/other/other?label=' + name + '&index=' + index,
          })

        }
      }else{
        wx.showModal({
          title: '提示',
          content: '服务暂停中',
          showCancel: false,
          confirmText: '朕知道了',
          confirmColor: '#6887e1'
        })
      }
    }
  },
  navToArea(){
    wx.navigateTo({
      url: '/pages/area/area',
    })
  },
  publist(){
    wx.navigateTo({
      url: '/pages/pub/pub',
    })
  },
  onLoad: function (options) {
    _this = this
    this.login()
    this.get_course()
    
  },
  getCarousel(){
    app.com.post('calousels/get',{a_id:wx.getStorageSync("area").pk_id},function(res){
      if(res.code == 1){
        _this.setData({
          imgurls:res.data.list
        })
      }
    })
  },
  login(){
    wx.showLoading({
      title: '加载中',
      task:true
    })
    app.login(function (res) {
      wx.hideLoading()
      wx.stopPullDownRefresh()
      app.getRes(res.data.id)
      app.getMoren(res.data.default_address)
      // _this.getCarousel()
      if (_this.data.isFirst) {
        
        _this.checkArea()
        _this.setData({
          isFirst: false
        })
      }
    })
  },
  onPullDownRefresh(){
    this.login()
    this.getServer(wx.getStorageSync('dl').pk_id) //获取服务
  },
  onShow(){
    if(!this.data.isFirst){
      this.checkArea()
      this.getServer(wx.getStorageSync('dl').pk_id) //获取服务
    }
    if(!this.data.emer){
      this.getAdminMemr()
    }
  },
  //通知
  getAdminMemr(){
    app.com.post('user/get/emer', { dl_id: 1 }, function (res) {
      _this.setData({
        emer: res.data
      })
      if (res.data.open_emer == 1) {
        wx.showModal({
          title: res.data.emer_title,
          content: res.data.emer_content,
          showCancel: false,
          confirmText: '朕知道了',
          confirmColor: '#6887e1'
        })
      }else{
        _this.getMemr()
      }
    })
  },
  getMemr(){
    app.com.post('user/get/emer',{dl_id:wx.getStorageSync("dl").pk_id || 1},function(res){
      _this.setData({
        emer:res.data
      })
      if(res.data.open_emer == 1){
        wx.showModal({
          title: res.data.emer_title,
          content: res.data.emer_content,
          showCancel:false,
          confirmText:'朕知道了',
          confirmColor:'#6887e1'
        })
      }
    })
  },
  getServer(id) {
    app.com.post('server/get/uid', {
      uid: id
    }, function (res) {
      if (res.code == 1) {
        wx.setStorageSync("server", res.data)
        _this.setData({list:res.data})
      
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    })
  },
  get_course(){
    let xh = wx.getStorageSync('res').xh
    let mm = wx.getStorageSync('res').mm
    if(xh != '' && mm != ''){
      wx.request({
        // url: `${domain}/edu/course`,
        url: `${domain}/get_schedule`,
        // data: { uid: uid, pwd: password, cookie: app.globalData.edusysUserInfo.cookie }, //温世珑
        data: { 
          wxid: wx.getStorageSync('user').id,
          xh,
          mm, 
          year:this.data.schoolYear,
          term:this.data.schoolTerm
        },
        method: 'POST',
        success: function(res){
          if(res.statusCode == 200){
            // console.log(res.data)
            // 将课表存放在本地
            wx.setStorage({ data: res.data.lessons, key: 'myCourse' })
            wx.setStorage({ data: res.data, key: 'myCourseJson' })
            wx.setStorage({ data: res.data.schoolYear.substring(0,4), key: 'schoolYear' })
            wx.setStorage({ data: res.data.schoolYear.substring(0,4), key: 'schoolTerm' })
            // wx.showToast({ title: '已自动更新最新课表', icon: 'none' })
            // wx.vibrateShort({ type: 'medium' })
          }
        }
      })
   }
  },
  checkArea() {
    if (wx.getStorageSync('area')) {
      this.setData({
        area: wx.getStorageSync('area'),
      })
      if(_this.data.imgurls.length == 0){
        _this.getCarousel()
      }
      if(_this.data.list.length == 0){
        _this.getServer(wx.getStorageSync('dl').pk_id)
      }

    } else {
      wx.navigateTo({
        url: '/pages/area/area',
      })
    }
  },
  onShareAppMessage(){
    return {
      title:'圈圈啦',
      path:'/pages/index/index'
    }
  }
  
})
